import type {Page, Result} from "@/api/common-types.ts";
import request from "@/utils/http";
import type {DbTable, GlobalDataSource} from "@/api/datasource/type.ts";

/**
 * 获取数据源列表
 *
 * @returns 校验图片
 */
export function findDataSourceListApi(page: Page): Promise<Result<Array<GlobalDataSource>>> {
    return request({
        url: "/datasource/findDataSourceList",
        method: "get",
        params: {
            pageNum: page.pageNum,
            pageSize: page.pageSize,
        },
        headers: {
            "Content-Type": "application/json;charset=utf-8",
            "Use-Timestamp": true,
        },
    });
}

/**
 * 获取所有数据源下拉列表数据
 *
 * @returns 校验图片
 */
export function findSelectDatasourceListApi(): Promise<Result<Array<GlobalDataSource>>> {
    return request({
        url: "/datasource/findSelectDatasourceList",
        method: "get",
        headers: {
            "Content-Type": "application/json;charset=utf-8",
            "Use-Timestamp": true,
        },
    });
}

/**
 * 获取所有数据源下拉列表数据
 *
 * @returns 校验图片
 */
export function findAllTablesApi(page: Page): Promise<Result<Array<DbTable>>> {
    return request({
        url: "/datasource/tables",
        method: "get",
        params: {
            pageNum: page.pageNum,
            pageSize: page.pageSize,
        },
        headers: {
            "Content-Type": "application/json;charset=utf-8",
            "Use-Timestamp": true,
        },
    });
}

/**
 * 添加数据库
 *
 * @param data {LoginData}
 * @returns
 */
export function addDataSourceApi(data: GlobalDataSource): Promise<Result<string>> {
    const requestJsonData = JSON.stringify(data)
    return request({
        url: "/datasource/addDatasource",
        method: "post",
        data: requestJsonData,
        headers: {
            "Content-Type": "application/json;charset=utf-8",
        },
    });
}


